package com.example.a80205050.myapplication.Dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

import com.example.a80205050.myapplication.Modle.Tb_inaccount;

/**
 * Created by 80205050 on 2017/10/30.
 */

public class InaccountDao {
    private DBOpenHelper helper;//创建DBOpenHelper对象
    private SQLiteDatabase db;//创建SQLiteDataBase对象
    public InaccountDao(Context context) {
        helper=new DBOpenHelper(context);
    }
    /**
     * 添加收入信息
     * @param tb_inaccount
     */
    public void add(Tb_inaccount tb_inaccount){
        db=helper.getWritableDatabase();//初始化SQLiteDatabase
        db.execSQL("insert into  tb_inaccount(_id,money,time,type,handler,mark) values(?,?,?,?,?,?)",
                new Object[]{tb_inaccount.get_id(),tb_inaccount.getMoney(),tb_inaccount.getTime(),
                tb_inaccount.getType(),tb_inaccount.getHandler(),tb_inaccount.getMark()});
    }

    /**
     * 更新数据
     * @param tb_inaccount
     */
    public void update(Tb_inaccount tb_inaccount){
        db=helper.getWritableDatabase();//初始化SQLiteDatabase
        db.execSQL("update tb_inaccount set money=?,time=?,type=?,handler=?,mark=? where _id=?",
                new Object[]{tb_inaccount.getMoney(),tb_inaccount.getTime(),
                tb_inaccount.getType(),tb_inaccount.getHandler(),tb_inaccount.getMark(),tb_inaccount.get_id()});
    }

    /**
     * 根据id查找收入的数据
     * @param id
     * @return
     */
    public Tb_inaccount find(int id){
        db=helper.getWritableDatabase();
        Cursor cursor=db.rawQuery("select _id,money,time,type,handler,mark from tb_inaccount " +
                "where _id=?",new String[]{String.valueOf(id)});
        if(cursor.moveToNext()){
            return new Tb_inaccount(cursor.getInt(cursor.getColumnIndex("_id")),
                                    cursor.getDouble(cursor.getColumnIndex("money")),
                                    cursor.getString(cursor.getColumnIndex("time")),
                                    cursor.getString(cursor.getColumnIndex("type")),
                                    cursor.getString(cursor.getColumnIndex("handler")),
                                    cursor.getString(cursor.getColumnIndex("mark"))
                    );
        }
        return null;
    }

    /**
     * 删除收入信息
     * @param ids
     */
    public void delete(Integer... ids){
        if(ids.length>0){
            StringBuilder sb=new StringBuilder();
            for(int i=0;i<ids.length;i++){
                sb.append('?').append(',');
            }
            sb.deleteCharAt(sb.length()-1);
            db=helper.getWritableDatabase();
            db.execSQL("delete from tb_inaccount where _id in ("+sb+")",(Object[])ids);
        }
    }

    /**
     * 获取收入的信息
     * @param start 初始的位置
     * @param count  每页显示的数量
     * @return
     */
    public List<Tb_inaccount> getScrollData(int start,int count){
        List<Tb_inaccount> tb_inaccountList=new ArrayList<Tb_inaccount>();//初始化集合对象
        db=helper.getWritableDatabase();
        Cursor cursor=db.rawQuery("select * from tb_inaccount limit ?,?",new String[]{
                String.valueOf(start),String.valueOf(count)
        });
        while (cursor.moveToNext()){
            tb_inaccountList.add(new Tb_inaccount(
                    cursor.getInt(cursor.getColumnIndex("_id")),
                    cursor.getDouble(cursor.getColumnIndex("money")),
                    cursor.getString(cursor.getColumnIndex("time")),
                    cursor.getString(cursor.getColumnIndex("type")),
                    cursor.getString(cursor.getColumnIndex("handler")),
                    cursor.getString(cursor.getColumnIndex("mark"))
            ));
        }
        return tb_inaccountList;
    }

    /**
     * 获取总的记录数
     * @return
     */
    public long getCount(){
        db=helper.getWritableDatabase();
        Cursor cursor=db.rawQuery("select count(_id) from tb_inaccount ",null);
        if(cursor.moveToNext()){
            return cursor.getLong(0);
        }
        return 0;
    }
    /**
     * 返回收入的最大编号
     * @return
     */
    public int getMaxId(){
        db=helper.getWritableDatabase();
        Cursor cursor=db.rawQuery("select max(_id) from tb_inaccount ",null);
        if(cursor.moveToLast()){
            return cursor.getInt(0);
        }
        return 0;//没有数据，返回0
    }



}
